package org.example.controllers;

import lombok.extern.slf4j.Slf4j;
import org.example.common.ApiResponse;
import org.example.common.NumberCheck;
import org.example.entitys.managers.Course;
import org.example.entitys.relationship.Unit;
import org.example.entitys.relationship.teaching_evaluation;
import org.example.service.inter.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @Description
 * @Author 魏锴
 * @Date 2023/11/24
 */

@Slf4j
@Controller
@ResponseBody
@RequestMapping(value = "/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @PostMapping("/create-course")
    public ApiResponse createCourse(String name, int activity_id) {
        Course courseVO = new Course(name, activity_id);
        log.info("createCourse(course = " + courseVO + ")");
        return courseService.createCourse(courseVO) ? ApiResponse.ok(courseVO) : ApiResponse.failure();
    }

    @PostMapping("/create-unit")
    //学习单元创建
    public ApiResponse createUnit(String title, String doc_path, String video_path, String unit_introduce,
                                  String father_id, int course_id, int score) {
        Unit unitVO = new Unit(title, doc_path, video_path, unit_introduce, father_id, course_id, score);
        log.info("createUnit(unit = " + unitVO + ")");
        return courseService.createLearnUnit(unitVO) ? ApiResponse.ok(unitVO) : ApiResponse.failure();
    }

    @PostMapping("/query-unit")
    public ApiResponse queryUnit(String id) {
        if (NumberCheck.isNotNumber(id)) return ApiResponse.paramIncorrect("id");
        log.info("queryUnit(id = " + id + ")");
        Unit unit = courseService.findUnitById(Integer.parseInt(id));
        return unit != null ? ApiResponse.ok("查询结果", unit) : ApiResponse.failure("数据库不存在id = " + id + "的unit");
    }

    @PostMapping("/evaluation")
    public ApiResponse createEvaluation(String SICI, String grade, String evaluation, String course_id, String evaluator) {
        if (NumberCheck.isNotNumber(SICI)) return ApiResponse.paramIncorrect("SICI");
        if (NumberCheck.isNotNumber(course_id)) return ApiResponse.paramIncorrect("course_id");
        if (NumberCheck.isNotNumber(evaluator)) return ApiResponse.paramIncorrect("evaluator");
        teaching_evaluation eva = new teaching_evaluation(-1, Integer.parseInt(SICI), grade, evaluation,
                new Date(), Integer.parseInt(course_id), Integer.parseInt(evaluator));
        log.info("createEvaluation(evaluation = " + eva + ")");
        return courseService.createEvaluation(eva) ? ApiResponse.ok(eva) : ApiResponse.failure();
    }

}
